package com.example.communist_app_api.controller.web;

import com.example.communist_app_api.db.dto.ActivityDTO;
import com.example.communist_app_api.db.dto.UserActivityDTO;
import com.example.communist_app_api.db.pojoDto.Activity;
import com.example.communist_app_api.exception.GlobalException;
import com.example.communist_app_api.service.ActivityService;
import com.example.communist_app_api.service.UserActivityService;
import com.example.communist_app_api.service.UserService;
import com.example.communist_app_api.util.CosUtils;
import com.example.communist_app_api.util.JwtUtils;
import com.example.communist_app_api.util.R;
import com.example.communist_app_api.vo.web.req.ActivityCreateReq;
import com.example.communist_app_api.vo.web.req.ActivitySelectReq;
import com.example.communist_app_api.vo.web.req.ActivityUpdateReq;
import com.example.communist_app_api.vo.web.req.AdditionalSignReq;
import com.github.pagehelper.PageInfo;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/26 15:01
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private UserService userService;

    @Autowired
    private UserActivityService userActivityService;



    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public R createPhoto(@ApiParam("图片")
                         @NotBlank @RequestBody MultipartFile multipartFile){
        String photoUrl = CosUtils.uploadFile(multipartFile, "");
        return R.ok("上传成功").put("photoUrl",photoUrl);
    }

    @ApiOperation("创建活动")
    @PostMapping("/create")
    public R createActivity(@RequestBody ActivityCreateReq req, HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String userName = claims.getSubject();
        Integer userId = userService.getUserInfo(userName).getId();
        Boolean status = activityService.createActivity(req, userId);
        if (status) {
            return R.ok("创建成功");
        }else {
            throw new GlobalException("新增活动失败");
        }
    }

    //原则上查的是同一个组织发布的活动（显示）

    /**
     *
     * @param req
     * @param request
     * @return
     */
    @ApiOperation("查询活动")
    @PostMapping("/show")
    public R showActivity(@RequestBody ActivitySelectReq req, HttpServletRequest request){
        //查询登录当前账号的userId
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String userName = claims.getSubject();
        Integer userId = userService.getUserInfo(userName).getId();

        Activity activity = new Activity();
        activity.setTitle(req.getTitle());
        activity.setStyle(req.getStyle());
        activity.setPlace(req.getPlace());

        List<Date> dateRange = req.getDateRange();

        if(dateRange!=null&&dateRange.size()==2){
            activity.setStartTime(dateRange.get(0));
            activity.setEndTime(dateRange.get(1));
        }

        PageInfo<ActivityDTO> pageInfo  = activityService.selectByCommon(activity,req.getPageNum(),req.getPageSize(),userId);
        return R.ok().put("pageInfo",pageInfo);
    }

    @ApiOperation("更新活动")
    @PostMapping("/update")
    public R updateActivity(@RequestBody @Valid ActivityUpdateReq req){
        Boolean flag = activityService.updateActivity(req);
        if (flag) {
            return  R.ok("更新成功");
        }else {
            return R.error("活动更新失败");
        }
    }

    @GetMapping("/delete")
    @ApiOperation("删除活动")
    public R deleteActivity(@RequestParam("id") @NotBlank Integer id){
        Integer delete = activityService.deleteActivity(id);
        if(delete ==0){
            throw new GlobalException("删除活动过程出错");
        }
        return R.ok("删除活动成功");
    }

    /**
     *
     * @param id 活动ID
     * @return 活动详情
     */
    @PostMapping("/selectActivityById")
    @ApiOperation("根据活动Id查询活动,查看详情")
    public R selectActivityById(@RequestParam("id") @NotBlank Integer id){
        ActivityDTO activityDTO = activityService.selectActivityById(id);
        if(activityDTO == null){
            throw new GlobalException("查询活动过程出错");
        }
        return R.ok("查询活动成功").put("activityDTO",activityDTO);
    }

    @GetMapping("/generateSignCode")
    @ApiOperation("生成签到验证码")
    public R generateSignLink(@RequestParam("activityId")  @NotBlank Integer activityId){
        String signCode=activityService.generateSignCode(activityId);
        return R.ok().put("signCode",signCode);
    }

    @GetMapping("/getSignUserList")
    @ApiOperation("获取签到情况")
    public R getSignUserList(@RequestParam("activityId")  @NotBlank Integer activityId){
        Map<Boolean, List<UserActivityDTO>> userMap=activityService.getSignUserList(activityId);
        return R.ok().put("map",userMap);
    }

    @PostMapping("/additionalSign")
    @ApiOperation("组织者补签")
    public R additionalSign(@RequestBody @Valid AdditionalSignReq signReq, HttpServletRequest request){
        String token = request.getHeader("token");
        Claims claims = JwtUtils.parseJWT(token);
        String organizersId = claims.getSubject();
        activityService.additionalSign(signReq.getUserId(),organizersId,signReq.getActivityId());
        return R.ok("补签成功");
    }





}
